package com.ld.security.shop.crawler.modules.re.crawler.dd;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.shop.crawler.modules.re.constant.CrawlerConstant;
import com.ld.security.shop.crawler.modules.re.crawler.BaseCrawler;
import com.ld.security.shop.crawler.modules.re.entity.ReBook;
import com.ld.security.shop.crawler.modules.re.entity.ReDetails;
import com.ld.security.shop.crawler.modules.re.service.IReBookService;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class DdReDetailsCrawler extends BaseCrawler<ReDetails> {

    @Autowired
    IReBookService iReBookService;

    public DdReDetailsCrawler() {
        super( CrawlerConstant.projectCode.dd,true);
    }

    @Override
    public ReDetails dataCrawler(String url) {
        ReDetails reDetails = super.dataCrawler(url);
//        Document document =  this.parse(reDetails.getContent());
//        Element content =  document.select(".product_main ").first();
//        reDetails.setContent(content.outerHtml());
        return reDetails;
    }


    public void parsing(String id) {
        ReDetails reDetails = iReDetailsService.getById(id);
        parsing(reDetails);
    }
        @Override
    public void parsing(ReDetails obj) {


        Document document =  this.parse(obj.getContent());
       Element largePic =  document.select("#largePic").first();
        Element title =  document.select(".name_info h1").first();
        Element subTitle =  document.select(".name_info .head_title_name").first();
        Element salePrice =  document.select("#dd-price").first();
        Element price =  document.select("#original-price").first();
        Element detailDescribe =  document.select("#detail_describe").first();

        Element isbn = detailDescribe.child(0).child(4);
         Element detailCategoryPath = document.select("#detail-category-path").first();


        Element messboxInfo =  document.select(".messbox_info").first();


        Element author = messboxInfo.child(0);
        Element publisher = messboxInfo.child(1);
        Element publishDate = messboxInfo.child(2);


        String _salePrice =  salePrice.text().replace("¥","");

        String _price= price.text().replace("¥","");
            ReBook reBook =  new ReBook();


        String _detailCategoryPath =    detailCategoryPath.text().replace("所属分类：","");

        String _publisher = publisher.text().replace("出版社:","");
            String    _author = author.text().replace("作者:","");


        reBook.setCoverPic(largePic.attr("src"));
        reBook.setTitle(title.text());
        reBook.setSubTitle(subTitle.text());
        reBook.setAuthorPenname(_author);
        reBook.setPublisher(_publisher);
        String dareStr = publishDate.text().substring(5).replace("年","-").replace("月","");
            if(isbn != null){
                String _isbn = isbn.text().replace("国际标准书号ISBN：","");
                reBook.setIsbn(_isbn);
            }else{
                Console.log("异常");
            }
            if(dareStr.length() == 8){
                Date data = DateUtil.parse(dareStr,"yyyy-mm").toJdkDate();

                reBook.setPublishDate(data );
            }else{
                Console.log("异常");
            }



        reBook.setSalePrice(Double.parseDouble(_salePrice));
        reBook.setPrice(Double.parseDouble(_price));
        reBook.setShortDescs(detailDescribe.html());

            reBook.setCategoryForSingleProductPageDesc(_detailCategoryPath);

        iReBookService.save(reBook);
    }



   public void getGoodsDetails(String... urls){
       for (String url : urls) {
           ReDetails reDetails =  super.dataCrawler(url);
           String json =  reDetails.getContent();
           Map data  =  JsonUtil.toObject(json,Map.class);

           reDetails.setContent(JsonUtil.toJson(data));

           iReDetailsService.save(reDetails);
       }



    }


}

